#!/bin/bash

# 修复useCollectionTask.ts的refetchInterval
cat > temp_collection_refetch.txt << 'EOF'
    refetchInterval: (data) => {
      // Only poll if task is still running
      if (data && (data.status === TaskStatus.RUNNING || data.status === TaskStatus.PENDING)) {
        return 5000 // 5 seconds
      }
      return false // Stop polling
    },
EOF

# 找到行号并替换
line_start=$(grep -n "refetchInterval: (data) => {" useCollectionTask.ts | cut -d: -f1)
# 删除旧的refetchInterval块(7行)
sed -i "${line_start},$((line_start+6))d" useCollectionTask.ts
# 在前一行后插入新内容
sed -i "$((line_start-1))r temp_collection_refetch.txt" useCollectionTask.ts

# 修复useExportTask.ts的refetchInterval
cat > temp_export_refetch.txt << 'EOF'
    refetchInterval: (data) => {
      // Poll if task is still running
      if (data && (data.status === ExportStatus.RUNNING || data.status === ExportStatus.PENDING)) {
        return 3000 // 3 seconds
      }
      return false // Stop polling when completed/failed
    },
EOF

line_start=$(grep -n "refetchInterval: (data) => {" useExportTask.ts | cut -d: -f1)
sed -i "${line_start},$((line_start+6))d" useExportTask.ts
sed -i "$((line_start-1))r temp_export_refetch.txt" useExportTask.ts

rm temp_collection_refetch.txt temp_export_refetch.txt
echo "refetchInterval blocks fixed"
